package com.springboot_blog.controller;

import com.springboot_blog.common.Result;
import com.springboot_blog.dto.RoleDTO;
import com.springboot_blog.enums.ErrorCodeEnum;
import com.springboot_blog.exception.CustomException;
import com.springboot_blog.service.RoleService;
import com.springboot_blog.vo.PageResult;
import com.springboot_blog.vo.RoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/admin/roles")
@RequiredArgsConstructor
@Tag(name = "角色管理", description = "角色相关接口")
public class RoleController {

    @Resource
    private RoleService roleService;

    @PostMapping("/list")
    @Operation(summary = "分页查询角色列表", description = "根据条件分页查询角色列表")
    public Result<PageResult<RoleVO>> getRoleList(RoleDTO queryDTO) {
        PageResult<RoleVO> roleList = roleService.getRoleList(queryDTO);
        return Result.success("查询成功", roleList);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取角色详情", description = "根据角色ID获取角色详情")
    public Result<RoleVO> getRoleById(@PathVariable Long id) {
        RoleVO roleVO = roleService.getRoleById(id);
        return Result.success("查询角色信息成功！", roleVO);
    }

    @PostMapping("")
    @Operation(summary = "添加角色详情", description = "创建新的角色")
    public Result<RoleVO> addRole(@Valid @RequestBody RoleDTO roleDTO) {
        boolean flag = roleService.addRole(roleDTO);
        if (flag) {
            return Result.success("添加角色详情成功！", null);
        } else {
            throw new CustomException(ErrorCodeEnum.SYSTEM_ERROR);
        }

    }

    @PutMapping("/{id}")
    @Operation(summary = "更新角色详情", description = "根据角色ID更新角色详情")
    public Result<RoleVO> updateRole(@PathVariable Long id, @RequestBody RoleDTO roleDTO) {
        boolean flag = roleService.updateRole(id, roleDTO);
        if (flag) {
            return Result.success("更新角色详情成功！", null);
        } else {
            throw new CustomException(ErrorCodeEnum.SYSTEM_ERROR);
        }

    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色详情", description = "根据角色ID删除角色详情")
    public Result<Void> deleteRole(@PathVariable Long id) {
        boolean flag = roleService.deleteRoleById(id);
        if (flag) {
            return Result.success("删除角色详情成功！", null);
        } else {
            throw new CustomException(ErrorCodeEnum.SYSTEM_ERROR);
        }
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除角色", description = "批量逻辑删除角色")
    public Result<Void> batchDeleteRoles(@RequestParam("ids") List<Long> ids) {
        boolean flag = roleService.batchDeleteRoles(ids);
        if (flag) {
            return Result.success("删除角色详情成功！", null);
        } else {
            throw new CustomException(ErrorCodeEnum.SYSTEM_ERROR);
        }
    }

    @GetMapping("/enabled")
    @Operation(summary = "获取启用的角色列表", description = "获取所有启用状态的角色，用于下拉选择")
    public Result<List<RoleVO>> getEnabledRoles() {
        List<RoleVO> roles = roleService.getEnabledRoles();
        return Result.success("查询启用角色列表成功！",roles);
    }

}
